/***************
 * User Organizations Panel
 */
AIR2.User.Organizations = function() {
    var isAdmin = (AIR2.USERINFO.type == "S");

    // MANAGER role in any organization? show the button
    var isManager = isAdmin;
    Ext.iterate(AIR2.USERAUTHZ, function(uuid,role,obj) {
        if (AIR2.AUTHZ.MANAGER & role) isManager = true;
    });

    var p = new AIR2.UI.Panel({
        colspan: 1,
        title: 'Organizations',
        showTotal: true,
        iconCls: 'air2-icon-organization',
        storeData: AIR2.User.ORGDATA,
        url: AIR2.User.USERURL + '/organizations',
        itemSelector: '.air2-user-org',
        tpl: new Ext.XTemplate(
            '<table><tpl for="."><tr class="air2-user-org">' +
              '<td>' +
                '<div class="fullname">{[AIR2.Format.orgNameLong(values.Organization, true)]}</div>' +
                '<span class="role">{[values.AdminRole.ar_name]}</span>' +
              '</td>' +
              '<td style="width:40px">' +
                '<tpl if="uo_home_flag"><span class="icons air2-icon-home" ext:qtip="Home Organization"></span></tpl>' +
                '<tpl if="uo_notify_flag"><span class="icons air2-icon-email" ext:qtip="Notifications On"></span></tpl>' +
              '</td>' +
            '</tr></tpl></table>',
            {compiled: true, disableFormats: true}
        ),
        editModal: {
            allowAdd: isManager,
            width: 600,
            items: {
                xtype: 'air2liveeditor',
                url: AIR2.User.USERURL + '/organizations',
                sortField: 'org_display_name',
                sortDir: 'ASC',
                pageSize: 50,
                allowEdit: true,
                allowDelete: true,
                hideButtons: true,
                editForm: {
                    labelAlign: 'top',
                    layout:'column',
                    items: [{
                        xtype: 'container',
                        columnWidth: .44,
                        layout: 'form',
                        items: {
                            xtype: 'air2searchbox',
                            id: 'air2-organization-picker',
                            name: 'org_uuid',
                            fieldLabel: 'Organization',
                            searchUrl: AIR2.HOMEURL + '/organization',
                            baseParams: {sort: 'org_display_name', excl_user: AIR2.User.USERUUID},
                            valueField: 'org_uuid',
                            displayField: 'org_display_name',
                            disabled: true,
                            formatComboListItem: function(values) {
                                return values.org_display_name;
                            },
                            listeners: {
                                'render': function(thisBox) {
                                    // add listener to filter out orgs the user cannot MANAGE
                                    if (!isAdmin) {
                                        thisBox.store.on('load', function(thisStore,recs,opts) {
                                            thisStore.each(function(rec) {
                                                var uuid = rec.get('org_uuid');
                                                if (!(AIR2.AUTHZ.MANAGER & AIR2.USERAUTHZ[uuid])) {
                                                    //Logger("filter out non-MANAGER for " + uuid);
                                                    thisStore.remove(rec);
                                                }
                                                return true;
                                            });
                                        });
                                    }
                                }
                            }
                        }
                    },{
                        xtype: 'container',
                        columnWidth: .26,
                        layout: 'form',
                        items: {
                            xtype: 'air2combo',
                            name: 'uo_ar_id',
                            fieldLabel: 'Role',
                            store: new Ext.data.JsonStore({
                                data: AIR2.User.ROLELIST
                            }),
                            valueField: 'ar_id',
                            displayField: 'ar_name',
                            width: 130
                        }
                    },{
                        xtype: 'container',
                        columnWidth: .3,
                        layout: 'form',
                        items: [{
                            xtype: 'checkbox',
                            name: 'uo_notify_flag',
                            fieldLabel: 'Notifications'
                        },{
                            xtype: 'checkbox',
                            name: 'uo_home_flag',
                            inputType: 'hidden'
                        },{
                            xtype: 'hidden',
                            name: 'uo_status',
                            value: 'A'
                        }]
                    }],
                    listeners: {
                        beforeedit: function(form, rec) {
                            var f = form.getForm();
                            f.setValues(rec.data);
                            var uuidfld = form.find('name', 'org_uuid')[0];
                            uuidfld.setDisabled(!rec.phantom);
                            uuidfld.setRawValue(rec.data.org_display_name);
                        },
                        beforesave: function(form, rec) {
                            var f = form.getForm();
                            f.updateRecord(rec);
                        }
                    }
                },
                tpl: new Ext.XTemplate(
                    '<div class="air2-user-org" style="width:100%">' +
                      '<div class="flags">' +
                        '<tpl if="uo_home_flag"><span class="icons air2-icon-home" ext:qtip="Home Organization"></span></tpl>' +
                        '<tpl if="uo_notify_flag"><span class="icons air2-icon-email" ext:qtip="Notifications On"></span></tpl>' +
                      '</div>' +
                      '<div class="org-role">' +
                        '<span class="fullname">{[AIR2.Format.userOrgLong(values)]}</span>' +
                        '<span class="role">{[values.AdminRole.ar_name]}</span>' +
                      '</div>' +
                      '<span class="meta">' +
                        '<span>{[AIR2.Format.userOrgShort(values)]}</span>' +
                        '<span class="datewho">Added on {[AIR2.Format.dateLong(values.uo_cre_dtim)]} ' +
                          'by {[AIR2.Format.userName(values.CreUser, true)]}</span>' +
                      '</span>' +
                      '<tpl if="this.canEdit(values)">' +
                        '<button class="air2-rowedit"></button>' +
                        '<button class="air2-rowdelete"></button>' +
                      '</tpl>' +
                    '</div>',
                    {
                        compiled: true,
                        disableFormats: true,
                        canEdit: function(values) {
                            if (isAdmin) return true;

                            var role = AIR2.USERAUTHZ[values.org_uuid];
                            if (AIR2.AUTHZ.MANAGER & role) {
                                return true;
                            }
                            else {
                                return false;
                            }
                        }
                    }
                )
            }
        }
    });
    return p;
}
